package com.xy.smartsms.service.carrierparam;

import android.text.TextUtils;

import com.xy.smartsms.db.carrierparam.OnlineConfigManager;
import com.xy.smartsms.db.carrierparam.entity.OnlineConfigItem;
import com.xy.smartsms.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.net.NetUtil;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.sdk.util.XyUtil;

/**
 * Created by kangwenbo on 2017/3/1.
 */

public class UpdateCarrierParamService {
    private final static String TAG = "UpdateCarrierParamService";

    /**
     * 向服务器发起请求
     *
     * @param queryMap
     * @param extend
     * @param callBack
     */
    public static void updateDataByNet(final String requestUrl, final Map<String, Object> queryMap, final Map<String, String> extend, final XyCallBack callBack) {
        if (queryMap != null && !queryMap.isEmpty()) {
            final String reqeustContent = createReqeustContent(queryMap, extend);
            Log.e(TAG, "cmcc_ronghe_net, req url:" + requestUrl + ", req data" + reqeustContent);
            if (StringUtils.isNull(reqeustContent)) {
                XyUtil.doXycallBackResult(callBack, new Object[]{Integer.valueOf(-10), "reqeustContent null"});
            } else {
                try {
                    NetUtil.executeServiceHttpRequest(requestUrl, reqeustContent, extend, callBack);
                } catch (Throwable var6) {
                    Log.e(TAG, "UpdateCarrierParamService updateDataByNet error:", var6);
                    XyUtil.doXycallBackResult(callBack, new Object[]{Integer.valueOf(-11), var6.getMessage()});
                }
            }
        } else {
            XyUtil.doXycallBackResult(callBack, new Object[]{Integer.valueOf(-10), "not reqeustContent"});
        }
    }

    public static String createReqeustContent(Map<String, Object> queryMap, Map<String, String> extend) {
        if (queryMap != null && !queryMap.isEmpty()) {
            try {
                List<String> keys = new ArrayList<String>(queryMap.keySet());
                JSONObject e = new JSONObject();
                for (String key : keys) {
                    e.put(key, queryMap.get(key));
                }
                return e.toString();
            } catch (Throwable var3) {
                Log.e(TAG, "UpdateCarrierParamService createReqeustContent error:", var3);
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * 判断解析后数据是否合法
     *
     * @param objects
     * @return
     */
    public static JSONObject isValidOfData(Object... objects) throws JSONException {
        if (objects == null || objects.length <= 1) {
            Log.e(TAG, "Data error");
            return null;
        }
        Log.e(TAG, "req data" + objects[1].toString());
        int flag = (Integer) objects[0];
        if (flag != 3) {
            Log.e(TAG, String.valueOf(objects[1]));
            return null;
        }

        String str = objects[1].toString();
        if (!TextUtils.isEmpty(str)) {
            JSONObject jb = new JSONObject(str);
            if (jb.has("code")) {
                if ("90001".equals(jb.optString("code")) || "90004".equals(jb.optString("code"))) {
                    return jb;
                } else {
                    Log.e(TAG, str);
                }
            }
        }
        return null;
    }

    public static void updateOnlineConfig(String key, int updateStatus) {
        OnlineConfigItem onlineConfigItem = new OnlineConfigItem();
        onlineConfigItem.setKey(key);
        onlineConfigItem.setLastUpdateTime(System.currentTimeMillis());
        onlineConfigItem.setLastUpdateStatus(updateStatus);
        OnlineConfigManager.getInstance().updateOrInsert(onlineConfigItem, onlineConfigItem.getContentValues());

        OnlineConfigManager.getInstance().updateStateMap(key, System.currentTimeMillis(), updateStatus);
    }

}
